package com.mall.action;

import com.github.pagehelper.PageInfo;
import com.mall.bean.Permission;
import com.mall.bean.QueryWhereVO;
import com.mall.bean.Role;
import com.mall.bean.User;
import com.mall.dao.PermissionMapper;
import com.mall.service.PermissionService;
import com.mall.service.RoleService;
import com.mall.uitl.JsonUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @author lwd1513
 * @date 2022-04-15 17 23
 * discription
 */
@RestController
@RequestMapping("/RoleController")
public class RoleController {
    @Autowired
    private PermissionService permissionService;

    @Autowired
    private RoleService roleService;

    @RequestMapping("getPermissionListByRoleId")
    public JsonUtil getPermissionListByRoleId(String roleId) {

        List<Permission> permissionList = permissionService.getPermissionListByRoleId(roleId);

        JsonUtil jsonUtil = new JsonUtil();

        jsonUtil.setData(permissionList);

        return jsonUtil;
    }

    @RequestMapping("getRoleList")
    public JsonUtil getRoleList() {
        List<Role> roleList = roleService.getRoleList();
        JsonUtil jsonUtil = new JsonUtil();
        jsonUtil.setData(roleList);
        return jsonUtil;
    }

    //queryAllRole
    @RequestMapping("queryAllRole")
    public JsonUtil queryAllRole(QueryWhereVO queryWhereVO, Role role) {
        JsonUtil jsonUtil = new JsonUtil();
        String currentPage = queryWhereVO.getCurrentPage();
        String pageSize = queryWhereVO.getPageSize();

        PageInfo<Role> pageInfo = roleService.queryDataByPage(Integer.parseInt(currentPage), Integer.parseInt(pageSize), role);

        jsonUtil.setData(pageInfo);

        return jsonUtil;
    }

    //getPermissionList
    @RequestMapping("getPermissionList")
    public JsonUtil getPermissionList() {
        List<Permission> list = permissionService.getPermissionList();
        JsonUtil jsonUtil = new JsonUtil();
        jsonUtil.setData(list);
        return jsonUtil;
    }

    //confirmAssignPermission
    @RequestMapping("confirmAssignPermission")
    public JsonUtil confirmAssignPermission(@RequestParam("roleId") Integer roleId, @RequestParam("pids") String pids) {
        JsonUtil jsonUtil = new JsonUtil();

        String[] pidArray = null;

        if (!pids.equals("")) {
            pidArray = pids.split("[,]");
        }
        int res = roleService.confirmAssignPermission(roleId,pidArray);
        jsonUtil.setMsg("分配权限成功");
        return jsonUtil;
    }

    //deleteRoleById
    @RequestMapping("deleteRoleById/{roleId}")
    public JsonUtil deleteRoleById(@PathVariable("roleId") Integer roleId) {
        JsonUtil jsonUtil = new JsonUtil();
        int res = roleService.deleteRoleById(roleId);
        jsonUtil.setMsg("删除角色信息成功");
        return jsonUtil;
    }

    //updateRoleById
    @RequestMapping("updateRoleById")
    public JsonUtil updateRoleById(Role role) {
        JsonUtil jsonUtil = new JsonUtil();
        int res = roleService.updateRoleById(role);
        jsonUtil.setMsg("角色信息更改成功");
        return jsonUtil;
    }

    //saveRole
    @RequestMapping("saveRole")
    public JsonUtil saveRole(Role role) {
        JsonUtil jsonUtil = new JsonUtil();
        int res = roleService.saveRole(role);
        jsonUtil.setMsg("添加角色信息成功");
        return jsonUtil;
    }
}
